package com.gym.controller.action.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.gym.controller.form.admin.InsertTrainerForm;
import com.gym.model.dao.ConnessioneException;
import com.gym.model.dao.DAOTrainer;
import com.gym.model.dao.MetodoDaoException;
import com.gym.model.dao.ViolazioneIntegritaException;
import com.gym.model.entity.Trainer;

public class InsertTrainer extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		InsertTrainerForm t = (InsertTrainerForm) form;
		
		//dai dati del form compongo l'oggetto Trainer da inserire:
		String trainerfirstname=t.getTrainerfirstname();
		String trainerlastname=t.getTrainerlastname();
		String trainerphone=t.getTrainerphone();
		String traineremail=t.getTraineremail();
		String trainerskill=t.getTrainerskill();
		String trainernote=t.getTrainernote();

		try {
			Trainer trainer = new Trainer(0,trainerfirstname, trainerlastname, trainerphone, traineremail, trainerskill, trainernote);

			DAOTrainer dao = new DAOTrainer();
			dao.insert(trainer);
			
			request.setAttribute("feedback", "OK, inserito il trainer "+trainerfirstname+" "+trainerlastname);
			
			return mapping.findForward("success"); 
			
		} catch (ConnessioneException | MetodoDaoException | ViolazioneIntegritaException e) {
			request.setAttribute("error", e.getMessage());
			return mapping.findForward("failure"); 
		}
		
	}
	
}
